import Vue from 'vue'
import VueRouter from 'vue-router'

// 在路由守卫模块里面想拿到vuex全局共享的数据信息 需要在路由模块里面导入vuex全局数据共享模块
import store from '@/store'
// 导入需要的路由组件
// import Reg from '@/views/reg.vue'
const Reg = () => import('@/views/reg.vue')
// import Login from '@/views/login.vue'
const Login = () => import('@/views/login.vue')
// import Main from '@/views/Main.vue'
const Main = () => import('@/views/Main.vue')
// import Home from '@/views/menus/home.vue'
const Home = () => import('@/views/menus/home.vue')

// import UserInfo from '@/views/menus/user/infouser.vue'
const UserInfo = () => import('@/views/menus/user/infouser.vue')

// import userAvatar from '@/views/menus/user/Avatar.vue'
const userAvatar = () => import('@/views/menus/user/Avatar.vue')
// import userPwd from '@/views/menus/user/Pwd.vue'
const userPwd = () => import('@/views/menus/user/Pwd.vue')

// import List from '@/views/article/List.vue'
const List = () => import('@/views/article/List.vue')

// import ArtList from '@/views/article/Artlist.vue'
const ArtList = () => import('@/views/article/Artlist.vue')
Vue.use(VueRouter)

const router = new VueRouter({
  // 路由规则的数组
  routes: [
    {
      path: '/',
      component: Main,
      redirect: '/home',
      children: [
        { path: 'home', component: Home },
        { path: 'user-info', component: UserInfo },
        { path: 'user-avatar', component: userAvatar },
        { path: 'user-pwd', component: userPwd },
        { path: 'art-cate', component: List },
        { path: 'art-list', component: ArtList }
      ]
    },
    { path: '/reg', component: Reg },
    { path: '/login', component: Login }

  ]
})
// 声明全局前置首位
router.beforeEach((to, from, next) => {
  if (to.path === '/reg' || to.path === '/login') {
    next()
  } else {
    const token = store.state.user.token
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
